﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProgrammerToolsKits.Interface;
using ProgrammerToolsKits.Comic.Interface;

namespace ProgrammerToolsKits.Comic.Content
{
    public class CCGallery : IComicProvider
    {
        private readonly HtmlAgilityPack.HtmlDocument document;
        private readonly  INetworkService service;
        public CCGallery(INetworkService service)
        {
            this.service = service;
            document = new HtmlAgilityPack.HtmlDocument();
        }

        /// <summary>
        /// 获取主站
        /// </summary>
        public string Host => "http://www.tuku.cc";

        /// <summary>
        /// 获取该漫画库的搜索方案,{0}为关键字,{1}为搜索页页码
        /// </summary>
        public string QueryScheme => "http://www.tuku.cc/search/index/nickname/{0}/p/{1}.html";

        public IComicPageEnumerator Reader => throw new NotImplementedException();

        public async Task<IReadOnlyList<IComicBook>> FindComic(string name, int page = 0)
        {
            try
            {
                var content = await service.GetHtml( string.Format( QueryScheme, new object[] { name, page } ) );
                document.LoadHtml( content );
                var lists = document.DocumentNode.SelectNodes( "/html/body/div[9]/div[2]/div/div[2]/div" );
                var list = new List<IComicBook>();
                foreach ( var i in lists )
                {
                    var a1 = i.SelectSingleNode( "./a[1]" );
                    var img = i.SelectSingleNode( "./div/a/img" );
                    var tip = i.SelectSingleNode( "//p[2]/a" );

                    var thrumb = img.Attributes["src"].Value;
                    var title = img.Attributes["alt"].Value;
                    var description = tip.InnerText;
                    var src = Host + a1.Attributes["href"].Value;
                    list.Add( new Modal.ComicBook()
                    {
                        Source = src,
                        Description = description,
                        Thrumb = thrumb,
                        Title = title
                    } );
                }
                return list;
            }
            catch
            {
                return null;
            }
        }

        public async Task<IDetailPage> GetDetailPage(IComicBook book)
        {
            try
            {
                var content = await service.GetHtml( book.Source );
                document.LoadHtml( content );
                var mainForm = document.DocumentNode.SelectSingleNode( "/html/body/div[9]/div[2]" );

                var info = mainForm.SelectSingleNode( "./div[1]" );
                var div = info.SelectSingleNode( "./div[2]" );
                var lists = mainForm.SelectNodes( "./div[2]/div[3]/div[1]/a" );

                //Info 
                var thrumb = info.SelectSingleNode( "./div[1]/div/img" ).Attributes["src"].Value;


                var title = div.SelectSingleNode( "./p[1]" ).InnerText;
                var autor = div.SelectSingleNode( "/html/body/div[9]/div[2]/div[1]/div[2]/p[2]/span[1]" ).InnerText;
                var des = div.SelectNodes( "/html/body/div[9]/div[2]/div[1]/div[2]/p" );
                string descr = "";
                for ( int i = 3; i < des.Count; i++ )
                {
                    descr += des[i].InnerText;
                }

                var sb = new StringBuilder();
                var list = new List<Modal.ComicVolume>();
                foreach ( var i in lists )
                {
                    var src = i.Attributes["href"].Value;
                    var t = i.InnerText;
                    foreach ( var n in t )
                    {
                        if ( char.IsDigit( n ) )
                            sb.Append( n );
                    }
                    list.Add( new Modal.ComicVolume()
                    {
                        Index = int.Parse( sb.ToString() ),
                        Source = Host+src,
                        Title = t
                    } );
                    sb.Clear();
                }

                return new Modal.ComicDetail()
                {
                    Description = descr,
                    Thrumb = thrumb,
                    Title = title,
                    Author = autor,
                    Story = list,                    
                };
            }
            catch
            {
                return null;
            }

        }
    }
}
